package com.dragon.modules.yuegou.service.impl.system;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.system.RoleEntity;
import com.dragon.modules.yuegou.enums.OwnerType;
import com.dragon.modules.yuegou.enums.code.BusinessCode;
import com.dragon.modules.yuegou.enums.system.RoleStatus;
import com.dragon.modules.yuegou.enums.system.RoleType;
import com.dragon.modules.yuegou.mapper.system.RoleMapper;
import com.dragon.modules.yuegou.model.system.Role;
import com.dragon.modules.yuegou.query.system.RoleQuery;
import com.dragon.modules.yuegou.service.system.RoleService;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统角色 服务实现
 *
 * @author pbuilder generated
 * @version 2.0
 */
@Slf4j
@Service("sysRoleService")
public class RoleServiceImpl
		extends ServiceImpl<RoleMapper, RoleEntity>
		implements RoleService {

	@Override
	public void saveRole(Role role) {
		Check.notNull(role.getRoleName(), "角色名称不能为空");
		//判断该角色名是否已经存在
		RoleQuery roleQuery = new RoleQuery();
		roleQuery.setRoleName(role.getRoleName());
		List<RoleEntity> list = list(roleQuery.toQueryWrapper());
		if (!StringUtils.isEmpty(role.getId())) {
			
			if (list.size() > 0 && !list.get(0).getId().equals(role.getId())) {
				throw new DragonException(BusinessCode.ROLE_NAME_EXISTS);
			}
			//修改角色
		}else {
			//添加角色
			if (list.size() > 0) {
				throw new DragonException(BusinessCode.ROLE_NAME_EXISTS);
			}
			role.setStatus(RoleStatus.ENABLED);
			if(role.getType() == null) {
				role.setType(RoleType.USER);
			}
			role.setOwnerType(OwnerType.PLATFORM);
			
		}
	}

	@Override
	public void saveGroupRole(Role role) {
		Check.notEmpty(role.getRoleName(), "角色名称不能为空");
		//判断该角色名是否已经存在
		RoleQuery roleQuery = new RoleQuery();
		roleQuery.setRoleName(role.getRoleName());
		roleQuery.setOwnerType(OwnerType.GROUP);
		roleQuery.setGroupId("");
		roleQuery.setDel(false);
		if (!StringUtils.isEmpty(role.getId())) {
			List<RoleEntity> list = list(roleQuery.toQueryWrapper());
			if (list.size() > 0 && !list.get(0).getId().equals(role.getId())) {
				throw new DragonException(BusinessCode.ROLE_NAME_EXISTS);
			}
			//修改角色
		}else {
			Long count = count(roleQuery.toQueryWrapper());
			if (count>0) {
				throw new DragonException(BusinessCode.ROLE_NAME_EXISTS);
			}
			//添加角色
		}
	}

	@Override
	public Role selectRole(String roleId) {
		RoleEntity entity = getById(roleId);
		return new Role(entity);
	}

	
	@Override
	public List<Role> searchCharacter(String rolename) {
		RoleQuery roleQuery = new RoleQuery();
		roleQuery.roleName().like(rolename);
		List<RoleEntity> entities = this.list(roleQuery.toQueryWrapper());
		return entities.stream().map(Role::new).collect(Collectors.toList());
	}

	@Override
	public IPage<Role> searchGroupRole(PageRequest page, RoleQuery query) {
		query.setOwnerType(OwnerType.GROUP);
		query.setGroupId("");
		query.setDel(false);
		IPage<RoleEntity> pageEntity = new Page<>(page.getPage(), page.getSize(), page.getCount());
		IPage<RoleEntity> pageEntites = baseMapper.selectPage(pageEntity, query.toQueryWrapper());
		return pageEntites.convert(Role::new);
	}

	@Override
	public List<Role> getGroupRoleList(){
		RoleQuery query = new RoleQuery();
		query.setGroupId("");
		query.setOwnerType(OwnerType.GROUP);
		query.setDel(false);
		query.setStatus(RoleStatus.ENABLED);
		List<RoleEntity> entities = this.list(query.toQueryWrapper());
		return entities.stream().map(Role::new).collect(Collectors.toList());
	}

	@Override
	public List<Role> userNoRole(String roleName,String userId) {
		return baseMapper.userNoRole(roleName,userId);
	}

	@Override
	public List<Role> userRole(String userId) {
		return baseMapper.userRole(userId);
	}

	@Override
	public List<Role> groupUserNoRole(String roleName, String userId,String groupId) {
		return baseMapper.groupUserNoRole(roleName,userId,groupId);
	}

}